<%@ page import="cn.tedu.oa.common.util.ShiroUtils" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>后台管理</title>
    <link rel="icon" href="${pageContext.request.contextPath}/lib/static/login/images/favicon.ico">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/lib/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/lib/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<div class="layui-fluid">
    <hr class="layui-bg-red">
    <div class="layui-row">
        <div class="layui-col-sm6  layui-col-sm-offset3">
            <form class="layui-form layui-form-pane" lay-filter="userEdit">
                <input type="hidden" name="id">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" autofocus required lay-verify="required|username" lay-verType="tips" placeholder="请输入用户名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <input type="hidden" name="deptId">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-block">
                        <select id="deptId" name="deptId" required lay-verType="tips" lay-verify="department" lay-filter="selectDept" lay-search>
                            <option value=""></option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" required lay-verify="required" lay-verType="tips" placeholder="请输入姓名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">手机号</label>
                    <div class="layui-input-block">
                        <input type="text" name="mobile" required lay-verify="required|phone" lay-verType="tips" placeholder="请输入手机号" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-block">
                        <input type="text" name="email" required lay-verify="required|email" lay-verType="tips" placeholder="请输入邮箱" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <input type="text" name="remark" lay-verType="tips" placeholder="请输入备注" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">启用</label>
                    <div class="layui-input-block">
                        <input type="checkbox" title="是否禁用" name="valid" lay-skin="switch" lay-text="启用|禁用" disabled value="0">
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    var baseUrl = '${pageContext.request.contextPath}/sys/user/';
    layui.use(['form', 'jquery'], function () {
        var form = layui.form;
        var $ = layui.$;

        $(function () {
            var deptId = "<%=ShiroUtils.getPrincipal().getDeptId()%>";
            $.post(
                baseUrl + "getDeptIdAndName",
                function (result) {
                    var dept = result.data;
                    $.each(dept, function (index, dept) {
                        /*修改时选中状态*/
                        if (dept.id == deptId) {
                            $("#deptId").append("<option selected value='" + dept.id + "'>" + dept.deptName + "</option>");
                        } else {
                            $("#deptId").append("<option value='" + dept.id + "'>" + dept.deptName + "</option>");
                        }
                    });
                    form.render('select');
                    return false;
                },
                "json"
            );

            $.post(
                baseUrl + "getUserById",
                {id:"<%=ShiroUtils.getPrincipal().getId()%>"},
                function (user) {
                    if (user) {
                        $("body").find("input[name='username']").val(user.username).attr("readonly", "");
                        $("body").find("input[name='deptName']").val(user.deptName).attr("readonly","");
                        $("body").find("input[name='name']").val(user.name).attr("readonly", "");
                        $("body").find("input[name='mobile']").val(user.mobile).attr("readonly", "");
                        $("body").find("input[name='email']").val(user.email).attr("readonly", "");
                        $("body").find("input[name='remark']").val(user.remark).attr("readonly", "");
                        if (user.valid == 0) {
                            $("body").find("input[name='valid']").attr("checked", "checked");
                        }
                        form.render();
                    } else {
                        layer.msg(result.msg, {icon: 5, anim: 6});
                    }
                },
                "json"
            );
        });
    });
</script>
</body>
</html>
